﻿using System;
using System.Linq;
using System.Windows.Forms;
using IGM.DataProvider;
using IGM.DataProvider.Entites;
using IGM.Framework.Commons;
using IGM.Framework.Constants;
using IGM.Presentation.IgmServiceReference;

namespace IGM.Presentation.Materials
{
    public partial class StudentsControl : UserControl
    {
        private readonly DomainServiceClient _domainServiceClient;

        #region Constructor and Load Data

        public StudentsControl()
        {
            InitializeComponent();

            _domainServiceClient = new DomainServiceClient();
        }

        private void StudentsControlLoad(object sender, EventArgs e)
        {
            LoadMajors();
            Dock = DockStyle.Top;
        }

        private void LoadMajors()
        {
            cboMajors.DataSource = _domainServiceClient.GetMajors().Cast<Major>().ToList();
            cboMajors.DisplayMember = "Name";
            cboMajors.ValueMember = "Id";
        }

        private void LoadStudents()
        {
            if (cboMajors.Items.Count > 0)
            {
                dgvStudents.DataSource =
                    _domainServiceClient.GetStudentsByMajorId(((Major) cboMajors.SelectedItem).Id).Cast<Student>().
                        ToList();
                //dgvStudents.DataSource =
                //    _domainServiceClient.GetStudentsByTerm(true).Cast<Student>().ToList();

                DataGridViewHelper.ShowHideColumns(dgvStudents, new[]
                                                                   {
                                                                       (string) typeof (Student).Properties().IsChecked,
                                                                       (string) typeof (Student).Properties().Name,
                                                                       "Code","IdMajor", "DateOfBirth", "Term", "Thesis"
                                                                   });
                DataGridViewHelper.ShowIsCheck(dgvStudents);
                DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvStudents, false);
                DataGridViewHelper.ChangeDgvHeaderText(dgvStudents, new[]
                                                                   { "IsChecked", "Code", "Name","IdMajor", "DateOfBirth", "Term", "Thesis"},
                                                                   new[]
                                                                   { "Chọn", "Mã sinh viên", "Họ và tên", "Ngành", "Ngày tháng năm sinh", "Sinh viên năm cuối", "Đề tài"}
                                                                   );

                StudentCurrentRowChanged();
            }
            else
                MessageBox.Show(ConstantMessages.CanNotFindItem, @"ngành");
        }

        private void StudentCurrentRowChanged()
        {
            if (dgvStudents.CurrentRow != null)
            {
                txtStudentName.Text = dgvStudents.CurrentRow.Cells["Name"].Value.ToString();
                txtStudentCode.Text = dgvStudents.CurrentRow.Cells["Code"].Value.ToString();
                txtThesis.Text = dgvStudents.CurrentRow.Cells["Thesis"].Value.ToString();
                ckbFinalYear.Checked = (bool) dgvStudents.CurrentRow.Cells["Term"].Value;
                dtpBirthday.Value = (DateTime)dgvStudents.CurrentRow.Cells["DateOfBirth"].Value;
            }

            else
            {
                txtStudentName.Text = txtStudentCode.Text = txtThesis.Text = string.Empty;              
                ckbAllStudents.Checked = false;
                dtpBirthday.Value = DateTime.Now;
            }
        }
        private void CboMajorsSelectedIndexChanged(object sender, EventArgs e)
        {
            LoadStudents();
        }        

        private void DgvStudentsCurrentCellChanged(object sender, EventArgs e)
        {
            StudentCurrentRowChanged();
        }

        private void DgvStudentsCellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvStudents == null ||
               dgvStudents.Columns[(string)typeof(Student).Properties().IsChecked] == null ||
               dgvStudents.Columns[(string)typeof(Student).Properties().IsChecked].Index != e.ColumnIndex)
                return;

            dgvStudents[e.ColumnIndex, e.RowIndex].Value =
                !(bool)dgvStudents[e.ColumnIndex, e.RowIndex].Value;

            ckbAllStudents.Checked = DataGridViewHelper.AllRowsIsCheck(dgvStudents);
        }

        private void CkbAllStudentsCheckedChanged(object sender, EventArgs e)
        {
            DataGridViewHelper.ApplyCheckedAllOrUncheckAll(dgvStudents, ckbAllStudents.Checked);
        }

        private void ReloadStudent(string stundentName)
        {
            LoadStudents();
            DataGridViewHelper.SetDgvCurrentCell(dgvStudents, stundentName);
            StudentCurrentRowChanged();
        }

        #endregion

        #region Add, Edit, Delete Data

        private void BtnAddStudentsClick(object sender, EventArgs e)
        {
            try
            {
                var student = new Student
                { 
                    Name = txtStudentName.Text.Trim(), Code = txtStudentCode.Text.Trim(), 
                    DateOfBirth = dtpBirthday.Value, Thesis = txtThesis.Text.Trim(), 
                    Term = ckbFinalYear.Checked, IdMajor = (int) cboMajors.SelectedValue

                };
                string message;

                //if (_domainServiceClient.InsertStudentsValidator(student, out message))
                //{
                //    _domainServiceClient.InsertStudents(student);

                //    ReloadStudent(student.Name);
                //}
                //else
                //    MessageBox.Show(message);
            }
            catch (Exception ex)
            {
                Logger.Write(null, ex);
            }
        }

        #endregion
    }
}
